﻿using DongFangGuoKai.TaskPlatform.Commn;
using DongFangGuoKai.TaskPlatform.Commn.Attributes;
using DongFangGuoKai.TaskPlatform.Domain;
using DongFangGuoKai.TaskPlatform.Service.LogApp;
using DongFangGuoKai.TaskPlatform.WebApi.Extensions;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq.Expressions;

namespace DongFangGuoKai.TaskPlatform.WebApi.Controllers
{
    /// <summary>
    /// 日志管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    public class LogManagementController : ControllerBase
    {

        [AutowiredProperty]
        public ILogService _logService { get; set; }


        /// <summary>
        /// 日志管理（分页查询）
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="level">日志级别</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetLogList/{pageIndex}/{pageSize}")]
        public async  Task<IActionResult> GetLogList(string? level, DateTime? startDate, DateTime? endDate, int pageIndex = 1, int pageSize = 10)
        {
          
            var expable = Expressionable.Create<Nlog>();
            expable.AndIF(!string.IsNullOrEmpty(level), x => level.Contains(x.Level));
            expable.AndIF(startDate != null, x => x.Logged >= startDate);
            expable.AndIF(endDate != null, x => x.Logged <= endDate);

            var res = await _logService.GetLogListAsync(pageIndex, pageSize, expable.ToExpression());

            return new JsonResult(res);
        }

        /// <summary>
        /// 获取日志级别
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetLogLevel")]
        public IActionResult GetLogLevel()
        {
            List<SelectListItem> levelListItem = new List<SelectListItem>()
            {
                 new SelectListItem() { Text="Trace", Value="Trace", Order=0},
                 new SelectListItem() { Text="Debug", Value="Debug", Order=1},
                 new SelectListItem() { Text="Info",  Value="Info",  Order=2},
                 new SelectListItem() { Text="Warn",  Value="Warn",  Order=3},
                 new SelectListItem() { Text="Error", Value="Error", Order=4},
                 new SelectListItem() { Text="Fatal", Value="Fatal", Order=5}
            };


            return new JsonResult(new ApiDataResult<List<SelectListItem>>()
            {
                Code = "200",
                Count = levelListItem.Count,
                Success = true,
                Data = levelListItem,
                Message = "日志级别列表"
            }
            );
        }

    }
}
